package cn.fengsweb.tokyotyrant
{
	import flash.utils.ByteArray;
	
	internal class PutMoreHandler extends AbstractHandler
	{
		private var _putMore:PutMore
		private var _resultArray:Array;
		private var _currentKey:String;
		
		public function PutMoreHandler(host:String, port:uint)
		{
			super(host, port);
		}
		
		public function connect(putMore:PutMore, ttr:TokyoTyrantResponder) : void
		{
			_ttc		= new TokyoTyrantConnection(_host, _port, false);
			_ttr		= ttr;
			_putMore	= putMore;
			_resultArray= new Array();
			
			iteratePutOne();
		}
		
		private function iteratePutOne():void
		{
			var key:String			= _putMore.key;
			var sendBytes:ByteArray	= _putMore.sendBytes;
			
			if(null != sendBytes){
				_currentKey	= key;
				defaultHandler(sendBytes, successHandler, failedHandler);
			}else{
				_ttr.result(_resultArray);
				_ttc.close();
			}
		}
		
		private function successHandler(recvBytes:ByteArray):void
		{
			_resultArray[_currentKey]	= true;
			
			iteratePutOne();
		}
		
		private function failedHandler():void
		{
			_resultArray[_currentKey]	= false;
			
			iteratePutOne();
		}
	}
}